package com.bulkmail.sender.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class MainFrame extends JFrame {

	JMenuBar menuBar;
	JMenu fileMenu;
	JMenu sessionMenu;
	JMenuItem exitMenu;
	JMenu helpMenu;
	JMenuItem newMenu;
	JMenuItem openMenu;
	JMenuItem saveMenu;
	JMenuItem saveAsMenu;
	JLabel titleLabel;
	JTextField fromTextField;
	JTextField toTextField;
	JTextField subjectTextField;
	JTextArea messageArea;
	JTextField attachmentTextField;
	JRadioButton smtpRadioButton;
	JRadioButton exchangeRadioButton;

	JTextField serverTextField;
	JTextField portTextField;
	JTextField userTextField;
	JPasswordField passwordField;

	JTextField noOfEmailTextField;
	JTextField timeBetweenTextField;
	JCheckBox counterCheckBox;
	JCheckBox randomCheckBox;

	JButton startButton;
	JButton stopButton;
	JLabel infoLabel;

	public MainFrame() {
		this.setSize(650, 550);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setJMenuBar(createMenuBar());
		this.setLayout(new BorderLayout());
		this.add(createMainPanel(), BorderLayout.CENTER);
		this.add(createRunPanel(), BorderLayout.SOUTH);
	}

	private Component createRunPanel() {
		JPanel runPanel = new JPanel();
		infoLabel = new JLabel("");
		startButton = new JButton("Start");

		ActionListener startListener = new StartActionListener(this);
		startButton.addActionListener(startListener);

		stopButton = new JButton("Stop");
		runPanel.add(infoLabel);
		runPanel.add(startButton);
		runPanel.add(stopButton);
		return runPanel;
	}

	private Component createMainPanel() {
		JPanel mainPanel = new JPanel(new BorderLayout());
		mainPanel.add(createSessionPanel(), BorderLayout.NORTH);
		mainPanel.add(createOptionsPanel(), BorderLayout.SOUTH);
		return mainPanel;
	}

	private Component createOptionsPanel() {
		JPanel optionsPanel = new JPanel(new BorderLayout());
		optionsPanel.setBorder(BorderFactory.createTitledBorder("Options"));
		optionsPanel.add(createInnerOptionsPanel(), BorderLayout.CENTER);

		return optionsPanel;
	}

	private Component createInnerOptionsPanel() {
		JPanel innerOptionsPanel = new JPanel(new GridLayout(0, 2, 20, 0));
		JPanel sendViaPanel = new JPanel(new BorderLayout());
		sendViaPanel.setBorder(new TitledBorder("Send via"));

		JPanel protocolPanel = new JPanel();
		JLabel protocolLabel = new JLabel("Protocol: ");
		smtpRadioButton = new JRadioButton("SMTP");
		smtpRadioButton.setSelected(true);
		exchangeRadioButton = new JRadioButton("Exchange");
		ButtonGroup group = new ButtonGroup();
		group.add(smtpRadioButton);
		group.add(exchangeRadioButton);
		protocolPanel.add(protocolLabel);
		protocolPanel.add(smtpRadioButton);
		protocolPanel.add(exchangeRadioButton);
		sendViaPanel.add(protocolPanel, BorderLayout.NORTH);

		JPanel userDetailPanel = new JPanel(new GridLayout(4, 2));
		JLabel serverLabel = new JLabel("Server");
		JLabel portLabel = new JLabel("Port");
		JLabel userLabel = new JLabel("User");
		JLabel passLabel = new JLabel("Password");
		serverTextField = new JTextField("");
		portTextField = new JTextField("");
		userTextField = new JTextField("");
		passwordField = new JPasswordField("");
		userDetailPanel.add(serverLabel);
		userDetailPanel.add(serverTextField);
		userDetailPanel.add(portLabel);
		userDetailPanel.add(portTextField);
		userDetailPanel.add(userLabel);
		userDetailPanel.add(userTextField);
		userDetailPanel.add(passLabel);
		userDetailPanel.add(passwordField);
		sendViaPanel.add(userDetailPanel, BorderLayout.SOUTH);

		JPanel multiplicationPanel = new JPanel(new GridLayout(6, 0));
		multiplicationPanel.setBorder(new TitledBorder("Multiplication"));
		JLabel noOfEmailLabel = new JLabel("Number of emails");
		JLabel timeBetweenLabel = new JLabel(
				"Time between emails (miliseconds)");
		noOfEmailTextField = new JTextField("1");
		timeBetweenTextField = new JTextField("1000");
		counterCheckBox = new JCheckBox("Add counter to subject");
		counterCheckBox.setSelected(true);
		randomCheckBox = new JCheckBox("Add random subject");
		randomCheckBox.setSelected(true);
		multiplicationPanel.add(noOfEmailLabel);
		multiplicationPanel.add(noOfEmailTextField);
		multiplicationPanel.add(timeBetweenLabel);
		multiplicationPanel.add(timeBetweenTextField);
		multiplicationPanel.add(counterCheckBox);
		multiplicationPanel.add(randomCheckBox);

		innerOptionsPanel.add(sendViaPanel);
		innerOptionsPanel.add(multiplicationPanel);
		return innerOptionsPanel;
	}

	private Component createSessionPanel() {
		JPanel sessionPanel = new JPanel();
		sessionPanel
				.setBorder(BorderFactory.createTitledBorder("Mail Content"));
		sessionPanel.setLayout(new BorderLayout());
		sessionPanel.add(createMailPanel(), BorderLayout.CENTER);
		sessionPanel.add(createAttachmentPanel(), BorderLayout.SOUTH);

		return sessionPanel;
	}

	private Component createMailPanel() {
		JPanel mailPanel = new JPanel();
		mailPanel.setLayout(new GridLayout(0, 2));

		JPanel mailDetailsPanel = new JPanel();
		mailDetailsPanel.setLayout(new GridLayout(7, 0, 4, 4));

		JLabel fromLabel = new JLabel("From: ");
		fromTextField = new JTextField("");
		mailDetailsPanel.add(fromLabel);
		mailDetailsPanel.add(fromTextField);
		JLabel toLabel = new JLabel("To: ");
		toTextField = new JTextField("");
		mailDetailsPanel.add(toLabel);
		mailDetailsPanel.add(toTextField);
		JLabel subjectLabel = new JLabel("Subject: ");
		subjectTextField = new JTextField("");
		mailDetailsPanel.add(subjectLabel);
		mailDetailsPanel.add(subjectTextField);

		JPanel messagePanel = new JPanel();
		JLabel messageLabel = new JLabel("Message: ");
		messageLabel.setHorizontalAlignment(JLabel.LEFT);
		messageArea = new JTextArea(7, 25);
		messageArea.append("");
		messageArea.setLineWrap(true);
		JScrollPane spane = new JScrollPane(messageArea,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		messagePanel.add(messageLabel);
		messagePanel.add(spane);

		mailPanel.add(mailDetailsPanel);
		mailPanel.add(messagePanel);
		return mailPanel;
	}

	private Component createAttachmentPanel() {
		JPanel attachmentPanel = new JPanel(new GridLayout(2, 0));
		JPanel attachmentViewPanel = new JPanel();
		JLabel attachmentLabel = new JLabel("Attachment:");
		attachmentPanel.add(attachmentLabel);
		JButton button = new JButton();
		Icon imgicon = new ImageIcon("src/attachment-icon.gif");
		button.setIcon(imgicon);

		attachmentTextField = new JTextField(50);
		attachmentTextField.setText("");
		ActionListener attachmentListener = new AttachmentListener(
				attachmentTextField);
		button.addActionListener(attachmentListener);
		attachmentViewPanel.add(attachmentTextField);
		attachmentViewPanel.add(button);
		attachmentPanel.add(attachmentViewPanel);

		return attachmentPanel;
	}

	private JMenuBar createMenuBar() {
		menuBar = new JMenuBar();

		fileMenu = new JMenu("File");

		newMenu = new JMenuItem("New");
		openMenu = new JMenuItem("Open");

		ActionListener openListener = new OpenActionListener(this);
		openMenu.addActionListener(openListener);

		saveMenu = new JMenuItem("Save");
		saveAsMenu = new JMenuItem("Save As");

		ActionListener saveAsListener = new SaveAsActionListener(this);
		saveAsMenu.addActionListener(saveAsListener);

		fileMenu.add(newMenu);
		fileMenu.add(openMenu);
		fileMenu.add(saveMenu);
		fileMenu.add(saveAsMenu);

		fileMenu.addSeparator();

		exitMenu = new JMenuItem("Exit");
		exitMenu.addActionListener(new AppExitListener());
		fileMenu.add(exitMenu);

		helpMenu = new JMenu("Help");
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);

		return menuBar;
	}
}
